/*
 * @Author: xiaosihan 
 * @Date: 2024-05-27 15:22:01 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-05-29 04:20:35
 */

import { ProvincesData } from "@views/home/type";
import { BoxGeometry, Group, Mesh, MeshBasicMaterial, Vector3 } from "three";
import { GradientShader } from "../../shader/GradientShader";
import provincesDataJSON from "../provincesData.json";
import threeUtils from "three-base/threeUtils";
import Bar, { BarData } from "./Bar";
import homeStore from "@views/home/homeStore";

//柱状图组
export default class BarGroup extends Group {
    constructor() {
        super();
    }

    // 初始化
    init() {
        this.setData(provincesDataJSON);
    }

    //地图偏移量
    offset = new Vector3(-116.468994140625 * 5, 0, 40.25177764892578 * 5);

    // 设置柱状图数据
    setData(provincesDatas: Array<ProvincesData>) {
        this.clear();

        const factor = 0.7
        const height = 4.0 * factor;
        const max = Math.max(...provincesDatas.map(p => p.value));

        const barDatas: Array<BarData> = provincesDatas
            .filter((item, index) => item.value > 10)
            .concat([
                {
                    "name": "东城区",
                    "enName": "东城区",
                    "center": [
                        116.418757,
                        39.917544
                    ],
                    "centroid": [
                        116.416739,
                        39.912912
                    ],
                    "value": 9
                }
            ])
            .map((city, i) => {
                let geoHeight = height * (city.value / max);
                return {
                    name: city.name,
                    enName: city.enName,
                    value: city.value,
                    index: i,
                    height: geoHeight,
                    color: i > 3 ? "#fffef4" : "#77fbf5",
                    center: city.center,
                    centroid: city.centroid
                }
            });

        homeStore.setBarDatas(barDatas);

        // 创建飞线焦点
        barDatas.map((city, i) => {
            let [x, y] = city.centroid;
            let point = new Vector3(x, 0.1, -y).multiplyScalar(5).add(this.offset);
            let geoHeight = height * (city.value / max);

            //创建柱子模型
            const bar = new Bar({
                name: city.name,
                enName: city.enName,
                value: city.value,
                index: i,
                height: geoHeight,
                color: i > 3 ? "#fffef4" : "#77fbf5",
                center: city.center,
                centroid: city.centroid
            });

            new GradientShader(bar.material, {
                uColor1: i > 3 ? 0xfbdf88 : 0x50bbfe,
                uColor2: i > 3 ? 0xfffef4 : 0x77fbf5,
                size: geoHeight,
                dir: "y",
            });
            bar.position.copy(point);
            this.add(bar);
        });

    }

    updateMatrix(): void {
        super.updateMatrix();
    }
}